package com.cloud.drore.eboss.sso.controller;//package com.cloud.drore.eboss.sso.controller;
//
//import com.cloud.drore.eboss.sso.dao.UserRoleRelationRepository;
//import com.cloud.drore.eboss.sso.entity.UserInfo;
//import com.cloud.drore.eboss.sso.entity.UserRoleRelation;
//import com.cloud.drore.eboss.sso.shiro.entity.Token;
//import com.cloud.drore.eboss.sso.utils.CookieHelper;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.subject.Subject;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Date;
//import java.util.List;
//import java.util.stream.Collectors;
//
//@Slf4j
//@Controller
//@Api(value = "登录", description = "登录一些跳转")
//public class LoginController {
//
//	private final static String LOGIN_URL = "/html/login.html";
//	private final static String INDEX_URL = "/html/index.html";
//
//	@Resource
//	private UserRoleRelationRepository roleRelationRepository;
//
//	@GetMapping(value = "/login")
//	public String login(){
//		return "redirect:" + LOGIN_URL;
//	}
//
////	@ApiOperation(value = "登录", notes = "根据传入的账户和密码进行shiro登录")
////	@ApiImplicitParams({
////			@ApiImplicitParam(name = "account", value = "账户", required = true, dataType = "String"),
////			@ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")
////	})
////	@ApiResponse(code = 401, message = "登录失败")
////	@PostMapping(value = "/loginIn")
////	@ResponseBody
////	public ResultMessage loginIn(@RequestBody UserInfo userInfo,
////								 HttpServletRequest request,
////								 HttpServletResponse response){
////		ResultMessage msg = new ResultMessage();
////		Subject subject = SecurityUtils.getSubject();
////		try {
////			subject.login(new UsernamePasswordToken(userInfo.getUserName(), userInfo.getPassword()));
////		} catch (IncorrectCredentialsException e) {
////			msg.setMessage("账号密码错误，请重新登录");
////		} catch (AuthenticationException e) {
////			msg.setMessage("账号密码错误，请重新登录");
////		} catch (Exception e) {
////			msg.setMessage("login fail, try again");
////		}
////		//登录成功逻辑
////		UserInfo user = (UserInfo) subject.getPrincipal();
////		setCookies(request, response, user, false);
////		msg.setData(getRedirectUrl(request));
////		return msg;
////	}
//
//	@ApiOperation(value = "退出登录", notes = "跳转到登录页面，如果已经登录则退出登录")
//	@ApiResponse(code = 401, message = "登出失败")
//	@RequestMapping(value = "/logout", method = RequestMethod.GET)
//	public String logout(HttpServletRequest request, HttpServletResponse response) {
//		Subject subject = SecurityUtils.getSubject();
//		if (subject != null && subject.getPrincipal() != null) {
//			UserInfo user = (UserInfo) subject.getPrincipal();
//			log.info("用户：[{}] 登出。", user.getUserName());
//			// 清除 cookies，不清除将永久免登陆
//			CookieHelper.clearAllCookie(request, response);
//		}
//		return "redirect:" + LOGIN_URL;
//	}
//
//	@ApiOperation(value = "查询用户名", notes = "")
//	@ApiResponse(code = 401, message = "查询失败")
//	@RequestMapping(value = "/queryName", method = RequestMethod.GET)
//	@ResponseBody
//	public String queryName() {
//		Subject subject = SecurityUtils.getSubject();
//		if (subject != null && subject.getPrincipal() != null) {
//			UserInfo user = (UserInfo) subject.getPrincipal();
//			return user.getUserName();
//		}
//		return "用户名未知";
//	}
//
////	@ApiOperation(value = "一键登录", notes = "开发阶段使用")
////	@ApiResponse(code = 401, message = "登录失败")
////	@RequestMapping(value = "/onceLoginIn", method = {RequestMethod.GET, RequestMethod.POST})
////	@ResponseBody
////	public ResultMessage onceLoginIn(HttpServletRequest request, HttpServletResponse response) {
////		UserInfo userInfo = new UserInfo();
////		userInfo.setPassword("123");
////		userInfo.setUserName("cmt");
////		return loginIn(userInfo, request, response);
////	}
//
////	@RequestMapping(value = "/loginInSSO")
////	@ResponseBody
////	public String loginInSSO(String account, String password,
////	                         String redirectUrl,
////	                      HttpServletRequest request,
////	                      HttpServletResponse response) {
////		log.info("用户：[{}] 进行登录操作", account);
////		Msg msg = ValidUtils.validMsg(User.class, "account,password", account, password);
////		if(msg.hasError()){
////			return msg.toString();
////		}
////
////		Subject subject = SecurityUtils.getSubject();
////		try {
////			subject.login(new UsernamePasswordToken(account, password));
////		} catch (IncorrectCredentialsException e) {
////			msg.setError("账号密码错误，请重新登录");
////		} catch (AuthenticationException e) {
////			msg.setError("账号密码错误，请重新登录");
////		} catch (Exception e) {
////			msg.setError("login fail, try again");
////		}finally {
////			if(!msg.getErrorList().isEmpty())
////				return msg.toString();
////		}
////		//登录成功逻辑
////		User user = (User) subject.getPrincipal();
////		setCookies(request, response, user, false);
////		msg.setData(redirectUrl);
////		return msg.toString();
////	}
//
//
////	private String getRedirectUrl(HttpServletRequest request) {
////		SavedRequest savedRequest = WebUtils.getAndClearSavedRequest(request);
////		String url = INDEX_URL;
////		if (savedRequest != null && StringUtils.isNotBlank(savedRequest.getRequestUrl())
////				&& !"/".equals(savedRequest.getRequestUrl()) && savedRequest.getRequestUrl().indexOf("login.html") < 0) {
////			url = savedRequest.getRequestUrl().replaceFirst(request.getContextPath(), "");
////		}
////		return url;
////	}
//
//	private void setCookies(HttpServletRequest request, HttpServletResponse response, UserInfo user, boolean httpOnly) {
//		Token tk = new Token();
//		tk.setId(user.getId());
//
//		List<UserRoleRelation> userRoleRelations = roleRelationRepository.findByTenantIdAndUserId(user.getTenantId(),user.getId());
//		List<Long> roleIds = userRoleRelations.stream().map((t)->t.getRoleId()).collect(Collectors.toList());
//		tk.setRoleId(roleIds);
//		tk.setCreatedDate(new Date());
//		tk.setModifiedDate(new Date());
//		CookieHelper.setSSOCookie(request, response, tk, httpOnly);
//	}
//
//}
